package br.com.massai.model.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import br.com.massai.model.entity.ProductionGroup;
import br.com.massai.model.exceptions.SystemException;

@Stateless
public class ProductionGroupDAO extends GenericDAO<ProductionGroup> implements ProductionGroupRepository {

	private static final long serialVersionUID = -1392676734514942310L;
	private static final String QUERY_AUTOCOMPLETE = "SELECT p FROM ProductionGroup p WHERE LOWER(p.nameGroup) LIKE LOWER(:nameGroup) AND p.status =:status";

	public ProductionGroupDAO(){
		super(ProductionGroup.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductionGroup> autocompleteMethod(String str) throws SystemException {
		List<ProductionGroup> resultList = new ArrayList<>();
		try {
			resultList = em.createQuery(QUERY_AUTOCOMPLETE)
					.setParameter("nameGroup", "%"+str+"%")
					.setParameter("status", true)
					.getResultList();

		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<>(); 
		}
		return resultList;
	}
}